package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.service.RouteService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 路线的控制层
 * @author Administrator
 * 
 */
@Controller("routeAction")
// 保证当有请求的时候,都创建一个Action对象
@Scope(value="prototype")
public class RouteAction extends ActionSupport implements ServletResponseAware,ServletRequestAware{
	private HttpServletResponse response;
	private int page;
	private int rows;
	private String json;
	private HttpServletRequest request;
	private int rids[];
	private String json1;
	public String getJson1() {	return json1;}
	public void setJson1(String json1) {	this.json1 = json1; }
	public int[] getRids() {	return rids;}
	public void setRids(int[] rids) {this.rids = rids;}
	public String getJson() {	return json;	}
	public void setJson(String json) {	this.json = json;}
	public int getPage() { 	return page;  }
	public void setPage(int page) { 	this.page = page; }
	public int getRows() {	return rows; }
	public void setRows(int rows) {  this.rows = rows; }
	@Resource
	private  RouteService  routeService;
	//显示路线信息
	 public void showRouteMsg(){
		 JSONObject obj = routeService.showRouteMsg(page, rows);
		 response.setCharacterEncoding("utf-8");
		 try {
			response.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
   //展示路线起点配送点名称信息（动态加载配送点名称）
	public void findRstartMsg(){
		JSONArray obj = routeService.findDpnameMsg();
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//展示路线终点配送点名称信息（动态加载配送点名称）
	public void findRendMsg(){
		JSONArray obj = routeService.findDpnameMsg();
		response.setCharacterEncoding("utf-8");
		try {
			response.getWriter().write(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//添加路线信息
	public void addRouteMsg(){
		JSONObject jso = JSONObject.fromObject(json);
		System.out.println(json);
		String rname=jso.getString("rname");
		String rstart=jso.getString("rstart");
		String rend=jso.getString("rend");
		Boolean flag=routeService.addRouteMsg(rname,rstart,rend);
		response.setCharacterEncoding("utf8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write(flag+"");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//删除路线信息
	public void deleteRouteMsg(){
		String rids[]=request.getParameterValues("rids[]");
		routeService.deleteRouteMsg(rids);
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	//修改用户信息
	public void updateRouteMsg(){
		JSONObject jso = JSONObject.fromObject(json1);
		int rid = jso.getInt("rid");
		String rname=jso.getString("rname");
		String rstart=jso.getString("rstart");
		String rend=jso.getString("rend");
		routeService.updateRouteMsg(rid, rname, rstart, rend);
		response.setCharacterEncoding("utf8");
		PrintWriter pw;
		try {
			pw = response.getWriter();
			pw.write("");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request=request;
	}
}
